package com.pituruh.utils;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RouteXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = null;
	public static RouteSitesList routeList = null;
	public static ShelterSitesList shelterList = null;

	public static RouteSitesList getRouteList() {
		return routeList;
	}

	public static void setRouteList(RouteSitesList routeList) {
		RouteXMLHandler.routeList = routeList;
	}

	/**
	 * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("TJRoute")) {
			/** Start */
			routeList = new RouteSitesList();
		} else if (localName.equals("shelters")) {
			shelterList = new ShelterSitesList();
			//routeList.setCategory(attr);
		}

	}

	/**
	 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */
		if (localName.equalsIgnoreCase("name"))
			routeList.setName(currentValue);
		else if (localName.equalsIgnoreCase("sheltername"))
			shelterList.setName(currentValue);
		else if (localName.equalsIgnoreCase("latitude"))
			shelterList.setLatitude(currentValue);
		else if (localName.equalsIgnoreCase("longitude"))
			shelterList.setLongitude(currentValue);
		else if (localName.equalsIgnoreCase("distance"))
			shelterList.setDistance(currentValue);
		else if (localName.equalsIgnoreCase("shelters"))
			routeList.setShelters(shelterList);

	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}

}